package com.lingnan.wanxiangxinxun.controller.admin;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.wanxiangxinxun.entity.dto.query.Author_queryDTO;
import com.lingnan.wanxiangxinxun.entity.po.Author;
import com.lingnan.wanxiangxinxun.entity.vo.common.PageVO;
import com.lingnan.wanxiangxinxun.entity.vo.common.Result;
import com.lingnan.wanxiangxinxun.service.IAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author swl
 * @since 2025-04-07
 */
@Controller
@RequestMapping("/admin/author")
public class AuthorController {

    @Autowired
    private IAuthorService iAuthorService;


    //新增
    @PostMapping("/add")
    public Result addAuthor(@RequestBody Author author) {
        author.setCreateTime(LocalDateTime.now());
        iAuthorService.save(author);
        return Result.success();
    }


    //删除
    @DeleteMapping("/delete/{ids}")
    public Result deleteAuthor(@PathVariable List<Integer> ids) {
        iAuthorService.removeBatchByIds(ids);
        return Result.success();
    }


    //修改
    @PutMapping("/update")
    public Result updateAuthor(@RequestBody Author author) {
        iAuthorService.updateById(author);
        return Result.success();
    }


    //查找--分页查所有
    @GetMapping("/getAllInPage")
    public Result getAllAuthor(@RequestParam(defaultValue = "1") long current,
                               @RequestParam(defaultValue = "10") long size) {

        Page<Author> p = new Page<>(current, size);
        p.addOrder(OrderItem.desc("create_time"));   // 按创建时间降序排列
        Page<Author> page_MP = iAuthorService.page(p);
        PageVO<Author> allInPage = PageVO.of(page_MP);

        return Result.success(allInPage);
    }


    //查找--根据条件查
    @PostMapping("/getAllInCondition")
    public Result getAuthorInCondition(@RequestBody Author_queryDTO author_queryDTO) {
        PageVO<Author> allInCondition = iAuthorService.getInCondition(author_queryDTO);
        return Result.success(allInCondition);
    }


    //查找--根据id查询
    @GetMapping("/getById")
    public Result getAuthorById(@RequestParam List<Integer> ids) {
        List<Author> authorList = iAuthorService.getAuthorListByIds(ids);
        return Result.success(authorList);
    }

}
